package com.example.blue_classic_plus;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.Map;

public class EntityUtils {

    static Gson gson = new Gson();


    ///实体转map
    public static Map<String, Object> toMap(Object obj){
        String json = gson.toJson(obj);
        Type type = new TypeToken<Map<String, Object>>() {}.getType();
        Map<String, Object> map = gson.fromJson(json, type);
        System.out.println("EntityUtils.toMap: " + map.toString());
        return map;
    }

    ///map转entity
    public static <T> T toEntity(Map<String, Object> map,Class<T> classOfT) {
        String json = gson.toJson(map);
        return gson.fromJson(json, classOfT);
    }
}
